const Koa = require("koa");
const koaRouter = require("koa-router");
const koaStaticCache = require("koa-static-cache");
const koaBody = require("koa-body");

const server = new Koa();
const router = new koaRouter();
server.use(
    koaStaticCache({
        prefix: "/public",
        dir: "./public",
        dynamic: true,
        gzip: true,
    })
);

// router.post("/users", koaBody(), async (ctx, next) => {
//     console.log("body", ctx.request.body);
//     ctx.body = "users";
// });

// 文件上传的操作
router.post(
    "/users",
    koaBody({
        // 是否开启对formdata格式的解析
        multipart: true,
        formidable: {
            uploadDir: "./public/avatar",
            keepExtensions: true,
        },
    }),
    async (ctx, next) => {
        console.log("body", ctx.request.body);
        console.log(ctx.request.files);
        ctx.body = "users";
    }
);

server.use(router.routes());

server.listen("8080");
